package usf.gwt.ui.demo.tools;

public abstract class CodeFormatter {

	public static final CodeFormatter XML_FORMATER = new CodeFormatter() {
		@Override
		public String format(String code) {
			return formatXML(code);
		}
	};

	public abstract String format(String code);

	protected static final native String formatXML(final String xml)/*-{
		var reg = /(<[\w\s=_\.\-']+>[\w\s]*<\/[\w]+>)|(<[\w\s=_\.\-']+>)|(<\/[\w]+>)/g;
		var ind = @usf.gwt.ui.demo.tools.CodeFormatter::indent(I);
		var t=""; var sp=0;
		while(m=reg.exec(xml))
		    t += (m[1]!=null?ind(sp):m[2]!=null?ind(sp++):ind(--sp))+m[0]+'\n';
		return t;
	}-*/;
	protected static final native String indent(int sp)/*-{
	    var s = "";
	    for(i=0;i<sp;i++) s+= "\t";
	    return s;
	}-*/;

}
